package com.nextStep.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

//拦截器
public class ExecuteIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object obj =  request.getSession().getAttribute("LOGIN_DATA");
        if(obj==null ) {
            // 是ajax请求
            if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {

                //告诉ajax我是重定向
                response.setHeader("REDIRECT", "REDIRECT");
                //告诉ajax我重定向的路径
                response.setHeader("CONTENTPATH", "error.jsp");
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                System.out.println("interceptors拦截");
                return false;
            } else {
                response.sendRedirect("../error.jsp");
                System.out.println("interceptors拦截2");
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
}
